package explorandum;
///* 
// * 	$Id: Tournament.java,v 1.2 2007/11/14 08:07:16 johnc Exp $
// * 
// * 	Programming and Problem Solving
// *  Copyright (c) 2007 The Trustees of Columbia University
// */
//package colony;
//
//import java.io.File;
//import java.io.FileOutputStream;
//import java.io.FileWriter;
//import java.io.IOException;
//import java.util.ArrayList;
//import java.util.Iterator;
//
//public final class Tournament {
//	private GameEngine engine;
//	private ArrayList<TournamentListener>listeners;
//	private Logger log;
//	private ArrayList<TournamentResult> results;
//	public Tournament(GameEngine engine){
//		this.engine = engine;
//		results = new ArrayList<TournamentResult>();
//		listeners = new ArrayList<TournamentListener>();
//		log = new Logger(Logger.LogLevel.INFO, this.getClass());
//	}
//	
//	public void run(){
//		results.clear();
//		//File []boardList = engine.getConfig().getBoardList();
//		Class<Player> []players = engine.getConfig().getClassList();
//		final int nGames = engine.getConfig().getTournamentGames();
//		final File boardFile = engine.getConfig().getSelectedBoard();
//		//TournamentResult[] results = new TournamentResult[players.length];
//		int matchNumber = 0;
//		for(int p = 0; p < players.length; p++){
//			TournamentResult result = play(players[p], boardFile, nGames);
//			if(result != null){
//				results.add(result);
//			}
//			matchNumber ++;
//		}	
//		
////		for(int i = 0; i < results.length; i++){
//	//		notifyListeners(results[i].toString());
////			System.out.println(results[i]);
//	//	}
//		//engine.removeGameListener(this);
//		
//	}
//	
//	private TournamentResult play(Class<Player> playerClass, final File boardFile, final int nGames){
//		engine.getConfig().setActivePlayer(playerClass);
//		engine.getConfig().setSelectedBoard(boardFile);
//		
//		if(engine.setUpGame()){ //TODO handle boolean retval
//			TournamentResult result = new TournamentResult(playerClass.toString(), boardFile.getName(), nGames);
//			notifyListeners("Current match information: Player \""+engine.getPlayerName()+"\" on board \""+boardFile.getName()+"\"");
//			for(int i = 0; i < nGames; i++){
//				notifyListeners("Playing game number " + i + " of "+nGames);
//				log.info("Playing game number " + i + " of "+nGames);
//				engine.setUpGame();
//				while(engine.step())
//					continue;
//				result.scores[i] = engine.getScore();
//			}
//			return result;
//		}
//		return null;
//	}
//	
//	public ArrayList<TournamentResult> getResults(){
//		return results;
//	}
//	
//	public void addTournamentListener(TournamentListener l){
//		listeners.add(l);
//	}
//	public void notifyListeners(String update){
//		Iterator<TournamentListener> it = listeners.iterator();
//		while(it.hasNext()){
//			it.next().tournamentUpdated(update);
//		}
//	}
//	static public interface TournamentListener{
//		public void tournamentUpdated(final String update);
//	}
//	
//	public class TournamentResult{
//		final String playerName;
//		final String boardName;
//		int[] scores;
//		TournamentResult(final String playerName, final String boardName, final int numGames){
//			this.playerName = playerName;
//			this.boardName = boardName;
//			scores = new int[numGames];
//		}
//		
//		public String toString(){		
//			int sum = 0;
//			String ret = playerName + "\t" + boardName;
//			ret += "\t" + engine.getConfig().getNumAnts();
//			ret += "\t" + engine.getConfig().getMaxRounds();
//			
//			for(int i = 0; i < scores.length; i++){
//				ret += "\t" + scores[i];
//				sum += scores[i];
//			}
//			ret +="\t"+ ((double)sum/(double)scores.length);
//			return ret;
//		}
//	}
//	
//	public static final void main(String args[]){
//		if(args.length != 5){
//			System.err.println("Usage: Tournament <config file> <board> <num ants> <num rounds> <num games>");
//			System.exit(-1);
//		}
//		GameEngine engine = new GameEngine(args[0]);
//		GameConfig config = engine.getConfig();
//		
//		File boardFile = new File(args[1]);
//		//File boardFile = new File(config. + File.separator+args[1]);
//		if(!boardFile.exists()){
//			System.err.println("Board file: " + boardFile + " does not exist.");
//			System.exit(-1);
//		}	
//		config.setSelectedBoard(boardFile);
//		config.setNumAnts(Integer.parseInt(args[2]));
//		config.setMaxRounds(Integer.parseInt(args[3]));
//		config.setTournamentGames(Integer.parseInt(args[4]));
//		
//		Tournament t = new Tournament(engine);
//		t.run();
//		ArrayList<TournamentResult> results = t.getResults();
//		for(TournamentResult r : results)
//			System.out.println(r);
//		
//		
//		File file = new File("tournament_"+boardFile.getName()+"_"+args[2]+"_"+args[3]+"_"+args[4]+".txt");
//		try{
//			FileWriter f = new FileWriter(file, false);
//			
//			for(TournamentResult r : results)
//				f.write(r.toString()+"\n");
//				//f.write("\n");
//			f.close();
//		}catch(IOException e){
//			System.err.println("Failed printing to output file:" + file );
//		}
//	}
//}
